package com.citybrain.rnapp.modules;

import com.citybrain.rnapp.MainApplication;
import com.citybrain.rnapp.R;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.hengqin.macao.activity.CameraScanActivity;
import com.hengqin.macao.activity.WebActivity;
import com.uuzuche.lib_zxing.AppLanguageUtils;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.bean.ZxingConfig;
import com.uuzuche.lib_zxing.common.Constant;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.RequiresApi;
import android.util.DisplayMetrics;

import java.util.Locale;

import static android.app.Activity.RESULT_OK;

public class ReactNativeQRCodeModule extends ReactContextBaseJavaModule implements ActivityEventListener
{

    ReactApplicationContext reactContext;
    private Callback QRCallBack;

    public ReactNativeQRCodeModule(ReactApplicationContext reactContext) {
        super(reactContext);

        this.reactContext = reactContext;
        this.reactContext.addActivityEventListener(this);
    }




    @ReactMethod
    public void openQR(String locale,final Callback resultCallBack) {


        Intent photoFrameIntent = new Intent(getActivity(), CameraScanActivity.class);
        ZxingConfig config = new ZxingConfig();
        // config.setPlayBeep(false);//是否播放扫描声音 默认为true
        //  config.setShake(false);//是否震动  默认为true
        // config.setDecodeBarCode(false);//是否扫描条形码 默认为true
        config.setReactColor(R.color.corner_color);//设置扫描框四个角的颜色 默认为白色
        // config.setFrameLineColor(R.color.colorAccent);//设置扫描框边框颜色 默认无色
         config.setScanLineColor(R.color.corner_color);//设置扫描线的颜色 默认白色
        config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
        photoFrameIntent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
        photoFrameIntent.putExtra("locale",locale);
        getActivity().startActivityForResult(photoFrameIntent, 1000);
        QRCallBack = resultCallBack;

    }

    public Activity getActivity() {
        return reactContext.getCurrentActivity();
    }


    @Override
    public String getName() {
        return "RNQRCode";
    }

    @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data)
    {
        if(requestCode==1000&&resultCode==RESULT_OK){

            WritableMap map = Arguments.createMap();
            Bundle bundle = data.getExtras();
            String result = bundle.getString(CodeUtils.RESULT_STRING);

            map.putString("data",result);
            QRCallBack.invoke(map);
        }

    }

    @Override
    public void onNewIntent(Intent intent)
    {

    }
}